extends Node

const EPSILON = 1e-8

var sceneFileDict
var imageFileDict
var playerInstance:PlayerScript

var coin = 500

var levelsConfig = {
	"2": { "health": 10, "damage": 2 },
	"3": { "health": 15, "damage": 3 },
	"4": { "health": 20, "damage": 4 },
	"5": { "health": 25, "damage": 5 },
	"6": { "health": 30, "damage": 6 }
}

var levelUpExpConfig = {
	"2": 5,
	"3": 12,
	"4": 21,
	"5": 32,
	"6": 45
}

var enemyExpConfig = {
	"E01": 2,
	"E02": 3,
	"E03": 5
}


func initLevelConfig():
	for i in range(2, 100):
		levelsConfig[str(i)] = {
			"health": 5 * i,
			"damage": i
		}
		
	levelUpExpConfig["1"] = 0
	for i in range(2, 100):
		levelUpExpConfig[str(i)] = levelUpExpConfig[str(i-1)] + 5 + (i - 2) * 2


func _ready():
	randomize()
	
	initLevelConfig()
	
	sceneFileDict = {
		"playerScene": preload("res://Scenes/Characters/Player.tscn"),
		"homeScene": preload("res://Scenes/Levels/Home.tscn"),
		"homeOutsideScene": preload("res://Scenes/Levels/HomeOutside.tscn"),
		"street01Scene": preload("res://Scenes/Levels/Street01.tscn"),
		"coinScene": preload("res://Scenes/Objects/Coin.tscn"),
		"bulletScene": preload("res://Scenes/Weapons/bullet.tscn"),
		"enemy01Scene": preload("res://Scenes/Characters/enemy_01.tscn"),
		"shopItem": preload("res://Scenes/UI/ShopItem.tscn"),
		"loot": preload("res://Scenes/Objects/Loot.tscn"),
		"hitEffect": preload("res://Scenes/Effect/HitEffect.tscn")
	}
	
	imageFileDict = {
		"ironRod": Image.load_from_file("res://Textures/weapons/ironrod.png"),
		"gun": Image.load_from_file("res://Textures/weapons/gun_ui.png"),
		"club": Image.load_from_file("res://Textures/weapons/club.png")
	}
	
	GlobalSignal.frameFreeze.connect(frameFreeze)
	
	
func gameStart():
	coin = 500
	GlobalSignal.coinCollected.emit()
	
	playerInstance = sceneFileDict["playerScene"].instantiate() as PlayerScript
	get_tree().root.add_child(playerInstance)
	playerInstance.z_index = 100
	playerInstance.changeWeapon("Club")
	movePlayerToScene("homeScene", "PlayerBornPosition")
	
	MainHUD.setVisible(true)
	

func restartGame():
	print("restartGame")
	GameEnd.setHide()
	MainHUD.setVisible(false)
	WeaponShop.closeWeaponShop()
	get_tree().change_scene_to_file("res://Scenes/UI/Title.tscn")
	var player = get_tree().get_first_node_in_group("Player")
	if player:
		player.queue_free()
	
	
func movePlayerToScene(sceneName, targetMarkerPath):
	get_tree().current_scene.queue_free()
	
	var sceneInstance = sceneFileDict[sceneName].instantiate() 
	get_tree().root.add_child(sceneInstance) 
	get_tree().current_scene = sceneInstance 
	
	var playerBornPosition:Marker2D = get_tree().current_scene.get_node(targetMarkerPath)
	playerInstance.setPosition(playerBornPosition.global_position)
	

func createCorn(position, parentNode):
	var coinNode = sceneFileDict["coinScene"].instantiate() as Node2D
	coinNode.z_index = 50
	parentNode.add_child(coinNode)
	coinNode.global_position = position
	

func shoot(position, dir, parentNode):
	var bulletNode = sceneFileDict["bulletScene"].instantiate() as Node2D
	bulletNode.z_index = 50
	parentNode.add_child(bulletNode)
	bulletNode.global_position = position
	bulletNode.scale.x = dir
	
	
func frameFreeze():
	var duration = 0.4
	var timeScale = 0.1
	Engine.time_scale = timeScale
	await get_tree().create_timer(duration * timeScale).timeout
	Engine.time_scale = 1.0
	

func createEnemy(position, parentNode):
	var enemy01Node = sceneFileDict["enemy01Scene"].instantiate() as Enemy01
	enemy01Node.z_index = 100
	parentNode.add_child(enemy01Node)
	enemy01Node.global_position = position
	var faces = [1, -1]
	enemy01Node.enemySpriteRoot.scale.x = faces[randi() % faces.size()] 


func createLoot(itemResName, position, parentNode:Node2D):
	var loot = sceneFileDict["loot"].instantiate() as Loot
	parentNode.add_child(loot)
	loot.init(itemResName, position)
	
	
func createHitEffect(position, parent:Node2D):
	var hitEffect = sceneFileDict["hitEffect"].instantiate() as HitEffect
	parent.add_child(hitEffect)
	hitEffect.init(position)
